<template>
	<view class="guide-detail-container">
		<text class="title">{{typeTitle}}</text>
		<view class="detail-box" v-for="data in articleDetail" :key="data.id">
				<view class="detail-text">
					<text class="detail-title">{{data.title}}</text>
					<rich-text class="detail-content" :nodes="formatContent(data.content)">
					</rich-text>
					<view class="detail-img" v-if="data.coverImg"><image :src="data.coverImg"></image></view>
				</view>
		</view>
	</view>
</template>

<script setup>
	import {getGuidelines} from '@/api/guide';
	import {onMounted, ref, computed} from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	
	const articleDetail = ref([]);
	
	const typeTitleMap = {
		1: '就诊前准备',
		2: '就诊流程',
		3: '就诊后',
		4: '其他问题'
	};
	
	const typeTitle = ref('');
	
	
	const formatContent = (content) => {
	  if (!content) return '';
	  // 转换换行符
	  return content
	    .replace(/</g, '&lt;')
	    .replace(/>/g, '&gt;')
	    .replace(/\n/g, '<br>');
	};
	
	onLoad(async (options) => {
		const type = options.type;
		typeTitle.value = typeTitleMap[type];
		await fetchArticleDetail(type);
	});
	
	
	const fetchArticleDetail = async (type) => {
		uni.showLoading({
			title: '加载中...'
		});
		try {
			const response = await getGuidelines({ type });
			if (response.code === "200") {
				articleDetail.value = response.data;
			}
		} catch (error) {
			console.error("获取文章详情失败:", error);
		} finally {
			uni.hideLoading({
			  success: (response) => {
			  },
			  complete: response => {
			  }
			});
		}
	};
	
</script>

<style lang="scss">
	.guide-detail-container{
		min-height: 100vh;
		padding: 30rpx;
		background-color: #F8F5FF;
		.title{
			font-size: 36rpx;
			font-weight: 700;
			color: #5713a2;
		}
		.detail-box{
			
		.detail-text{
			padding-bottom: 20rpx;
			margin-top: 20rpx;
			display: flex;
			flex-direction: column;
			gap: 10rpx;
			.detail-title{
				color: #7011a0;
				font-size: 30rpx;
			}
			.detail-content{
				font-size: 25rpx;
				line-height: 1.4;
			}
			.detail-img{
				width: 200rpx;
				height: 250rpx;
				margin: 0 auto;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
		}
	}
</style>